package c.c.c.a.i.a;

import android.app.Activity;
import android.app.Application;
import android.app.Application.ActivityLifecycleCallbacks;
import android.content.Context;
import android.os.Bundle;
import com.huawei.hms.jos.games.ranking.RankingConst;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* compiled from: ActivityDataManager */
public class b {
    public Application a;
    public Context b;
    public List<String> c = new ArrayList();
    public List<Long> d = new ArrayList();
    public List<String> e = new ArrayList();
    public List<Long> f = new ArrayList();
    public String g;
    public long h;
    public String i;
    public long j;
    public String k;
    public long l;
    public String m;
    public long n;
    public String o;
    public long p;
    public int q;
    public final ActivityLifecycleCallbacks r;

    /* compiled from: ActivityDataManager */
    public class a implements ActivityLifecycleCallbacks {
        public void onActivityCreated(Activity activity, Bundle bundle) {
            b.this.g = activity.getClass().getName();
            b.this.h = System.currentTimeMillis();
            b bVar = b.this;
            bVar.c.add(bVar.g);
            bVar = b.this;
            bVar.d.add(Long.valueOf(bVar.h));
        }

        public void onActivityDestroyed(Activity activity) {
            String name = activity.getClass().getName();
            int indexOf = b.this.c.indexOf(name);
            if (indexOf > -1 && indexOf < b.this.c.size()) {
                b.this.c.remove(indexOf);
                b.this.d.remove(indexOf);
            }
            b.this.e.add(name);
            b.this.f.add(Long.valueOf(System.currentTimeMillis()));
        }

        public void onActivityPaused(Activity activity) {
            b.this.m = activity.getClass().getName();
            b.this.n = System.currentTimeMillis();
            b bVar = b.this;
            bVar.q--;
        }

        public void onActivityResumed(Activity activity) {
            b.this.k = activity.getClass().getName();
            b.this.l = System.currentTimeMillis();
            b bVar = b.this;
            bVar.q++;
        }

        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
        }

        public void onActivityStarted(Activity activity) {
            b.this.i = activity.getClass().getName();
            b.this.j = System.currentTimeMillis();
        }

        public void onActivityStopped(Activity activity) {
            b.this.o = activity.getClass().getName();
            b.this.p = System.currentTimeMillis();
        }
    }

    public b(Context context) {
        a aVar = new a();
        this.r = aVar;
        this.b = context;
        if (context instanceof Application) {
            this.a = (Application) context;
        }
        Application application = this.a;
        if (application != null) {
            application.registerActivityLifecycleCallbacks(aVar);
        }
    }

    public final JSONObject a(String str, long j) {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put(RankingConst.RANKING_JGW_NAME, str);
            jSONObject.put("time", j);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jSONObject;
    }

    public final JSONArray b() {
        JSONArray jSONArray = new JSONArray();
        List list = this.c;
        if (!(list == null || list.isEmpty())) {
            int i = 0;
            while (i < this.c.size()) {
                try {
                    jSONArray.put(a((String) this.c.get(i), ((Long) this.d.get(i)).longValue()));
                    i++;
                } catch (Throwable unused) {
                }
            }
        }
        return jSONArray;
    }

    public final JSONArray c() {
        JSONArray jSONArray = new JSONArray();
        List list = this.e;
        if (!(list == null || list.isEmpty())) {
            int i = 0;
            while (i < this.e.size()) {
                try {
                    jSONArray.put(a((String) this.e.get(i), ((Long) this.f.get(i)).longValue()));
                    i++;
                } catch (Throwable unused) {
                }
            }
        }
        return jSONArray;
    }
}
